package sinaweibo;

import java.util.Properties;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import zzw.unicliinterfaces.CommentBlogInterface;
import zzw.unicliinterfaces.DeleteBlogInterface;
import zzw.unicliinterfaces.FavourBlogInterface;
import zzw.unicliinterfaces.ForwardBlogInterface;
import zzw.unicliinterfaces.GetTimeLinesInterface;
import zzw.unicliinterfaces.LoginInterface;
import zzw.unicliinterfaces.PublishBlogInterface;
import UniCli.SinaWeibo.CommentSinaWeibo;
import UniCli.SinaWeibo.DeleteBlogSinaWeibo;
import UniCli.SinaWeibo.FavoriteSinaWeibo;
import UniCli.SinaWeibo.GetTimeLinesSinaWeibo;
import UniCli.SinaWeibo.LoginSinaWeibo;
import UniCli.SinaWeibo.PublishSinaWeibo;
import UniCli.SinaWeibo.RepostSinaWeibo;

public class Activator implements BundleActivator {
	
	private ServiceRegistration sr=null; //add by zzw
	private String blogName = "SinaWeibo=新浪微博";//本身所在bundle的名字

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		System.out.println("register SinaWeibo login start");
		System.setProperty("UniCli.BlogSite", "renren=人人网");
		Properties props = new Properties();
		props.put("UniCli.BlogSite", "SinaWeibo");
		sr = bundleContext.registerService(LoginInterface.class.getName(), 
				new LoginSinaWeibo(), props);
		if(null != sr){
			System.out.println("register SinaWeibo login OK");
			String blogSites = System.getProperty("UniCli.BlogSite");
			System.out.println(blogSites);
			if(blogSites!=null){
				blogSites += ";";
				blogSites += blogName;
			}else{
				blogSites = blogName;
			}
			System.setProperty("UniCli.BlogSite", blogSites);
			//System.setProperties(props);
		}
		//这里没有失败处理
		sr = bundleContext.registerService(GetTimeLinesInterface.class.getName(), 
				new GetTimeLinesSinaWeibo(), props);
		sr = bundleContext.registerService(PublishBlogInterface.class.getName(), 
				new PublishSinaWeibo(), props);
		sr = bundleContext.registerService(CommentBlogInterface.class.getName(), 
				new CommentSinaWeibo(), props);
		
		sr = bundleContext.registerService(DeleteBlogInterface.class.getName(), 
				new DeleteBlogSinaWeibo(), props);
		sr = bundleContext.registerService(ForwardBlogInterface.class.getName(), 
				new RepostSinaWeibo(), props);
		sr = bundleContext.registerService(FavourBlogInterface.class.getName(), 
				new FavoriteSinaWeibo(), props);
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
	}

}
